<?php


namespace app\crontab\controller\once;


use custom\Mongo;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;

/**
 * 初中同步
 * Class SchoolMiddle
 * @package app\crontab\controller
 */
class SchoolMiddle  extends Command {


    protected function configure() {
        $this->setName('SchoolMiddle');
    }

    protected function execute(Input $input, Output $output) {
        echo "start\n";
        $mongo = Mongo::getInstance();
        $options = [
            'sort' => ['_id' => 1],
        ];
        $list = $mongo->select('wx_xqf_cz_list_data', [], $options);
        foreach ($list as $v1) {
            //详情
            $details = $mongo->select('wx_xqf_cz_details_data', ['id' => $v1->_id], []);
            $detail = $details[0];
            $lng = $detail->longitude ?? 0;
            $lat = $detail->latitude ?? 0;

            $school_property = self::school_property($detail->school_property);

            list($region, $pCodes, $areaCode) = self::location($detail->zone);

            //对口小学
            $primary_number = $mongo->getCount('SchoolPrimaryMiddle', ['cz_id' => (string)$detail->_id]);

            $data = [
                'mongo_id'           => (string)$detail->_id,
                'source_id'          => $detail->id,
                'name'               => $detail->name,
                'short_name'         => !empty($v1->short_name) ? $v1->short_name[0] : '',
                'province'           => $region['name1'],
                'city'               => $region['name2'],
                'area'               => $region['name3'],
                'city_code'          => $areaCode,
                'city_codes'         => $pCodes,
                'school_image'       => !empty($detail->school_image) ? join(',', $detail->school_image) : '',
                'school_phone'       => $detail->school_phone ?? '',
                'school_partition'   => $detail->school_partition ?? '',
                'tags'               => !empty($detail->tags) ? join(',', $detail->tags) : '',
                'school_property'    => $school_property,
                'address'            => $detail->address ?? '',
                'lng'                => $lng,
                'lat'                => $lat,
                'location'           => Db::raw("POINT({$lng}, {$lat})"),
                'school_stage'       => $detail->school_stage ?? '',
                'school_department'  => $detail->school_department ?? '',
                'education_feature'  => $detail->education_feature ?? '',
                'school_website'     => $detail->school_website ?? '',
                'school_intro'       => $detail->school_intro ?? '',
                'primary_number'     => $primary_number,
                'itime'              => time(),
            ];
            db("school_middle")->insert($data);
        }
        echo "end\n";
    }

    public static function school_property($params) {
        switch ($params) {
            case '公办':
                $school_property = 1;
                break;
            case '民办':
                $school_property = 2;
                break;
            case '国际':
                $school_property = 3;
                break;
            default:
                $school_property = 0;
                break;
        }
        return $school_property;
    }

    public static function location($params) {
        $areaCode = db('city_relation')
            ->where('new_name_school', $params)
            ->value('city_code');
        $region = db('region')
            ->alias('r1')
            ->join('region r2', 'r1.id=r2.pid')
            ->join('region r3', 'r2.id=r3.pid')
            ->where('r3.city_code', $areaCode)
            ->field('r1.name as name1,r2.name as name2,r3.name as name3,r1.city_code as city_code1,r2.city_code as city_code2,r3.city_code as city_code3')
            ->find();
        $pCodes = ",{$region['city_code1']},{$region['city_code2']},{$region['city_code3']}, ";
        return [
            $region,
            $pCodes,
            $areaCode
        ];
    }

}